package com.atguigu.day03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class Flink05_Transform_Connect {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);



        //2.获取两个流
        DataStreamSource<Integer> intStream = env.fromElements(1, 2, 3, 4, 5);


//        DataStreamSource<String> strStream = env.fromElements("a", "b", "c", "d", "e");
        DataStreamSource<String> strStream = env.socketTextStream("localhost", 9999);

        //TODO 3.使用Connect合并两条流
        ConnectedStreams<Integer, String> connect = intStream.connect(strStream);

        connect.map(new CoMapFunction<Integer, String, String>() {
//            private Integer stream1 = null;

            @Override
            public String map1(Integer value) throws Exception {
                System.out.println(System.currentTimeMillis());
                return value * value + "";
            }

            @Override
            public String map2(String value) throws Exception {
                System.out.println(System.currentTimeMillis());
                return value + " hello!";
            }
        }).print();

        env.execute();

    }
}
